Import libraries

rm(list = ls())
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

#install.packages("pxweb")
library(pxweb)
## Warning: package 'pxweb' was built under R version 4.1.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
library(corrr)
## Warning: package 'corrr' was built under R version 4.1.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.2
library(stargazer)
## Warning: package 'stargazer' was built under R version 4.1.2
#install.packages("plotly")
library(plotly)
## Warning: package 'plotly' was built under R version 4.1.3
#install.packages("ggthemes")
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.1.3
#install.packages("extrafont")
library(extrafont)
## Warning: package 'extrafont' was built under R version 4.1.3
library(stringr)

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

We start by importing the data set. Creating the data set is covered in a separate .r file. Thereafter, we wish to explore the data.

CPI_interest<-read.csv("CPI_interest.csv")

CPI_interest %>%
  ggplot(aes(x = KPI, y = inlåningsräntor, fill = avtal)) +
  geom_point(shape = 21) +
  labs(title = "Inflation mot räntor", x = "KPI", y = "Inlåningsräntor") +
  scale_fill_brewer(palette = "Set2", name = "Avtal") + 
  theme_fivethirtyeight() +
  theme(axis.title = element_text(), legend.box.background = element_rect(size = 0.8))
## Warning: Removed 334 rows containing missing values (geom_point).

Thereafter, we will gain some insight into the change over time.

p = CPI_interest %>%
  ggplot(aes(x = KPI, y = inlåningsräntor, fill = avtal)) +
  geom_jitter(shape = 21, size = 2, aes(frame = year, ids = inlåningsräntor)) +
  labs(title = "Inflation mot räntor", x = "KPI", y = "Inlåningsräntor", caption = "Nya och omförhandlade avtal började mätas i September 2005") +
  scale_fill_brewer(palette = "Set2", name = "Avtal") 
## Warning: Ignoring unknown aesthetics: frame, ids
fig = ggplotly(p) %>%
  animation_opts(
    frame = 800, transition = 400, redraw = TRUE, easing = "linear", mode = "next"
  ) %>% 
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  ) %>%
  animation_slider(
    currentvalue = list(prefix = "År: ", font = list(color="grey"))
  ) %>%
  animation_opts(1000, redraw = FALSE)

fig
base = CPI_interest %>%
  plot_ly(x = ~KPI, y = ~inlåningsräntor, color = ~avtal)

base %>%
  add_markers(
    alpha = 0.2, alpha_stroke = 0.2
  ) %>% 
  add_markers(frame = ~year, showlegend = T) %>%
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  ) %>%
  animation_slider(
    currentvalue = list(prefix = "År: ", font = list(color="grey"))
  ) %>%
  animation_opts(frame = 1000, transition = 400, redraw = FALSE, easing = "linear", mode = "next")
## Warning: Ignoring 334 observations

## Warning: Ignoring 334 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length
# make a correlation matrix 
correlations <- CPI_interest %>%
  select_if(is.numeric) %>%
  correlate()


# make some regressions, preferably with some control variables but I don't have that yet 
reg1 <- lm(data = CPI_interest, inlåningsräntor ~ KPI)
stargazer(reg1, type = "text") 
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                           inlåningsräntor      
## -----------------------------------------------
## KPI                          0.589***          
##                               (0.033)          
##                                                
## Constant                      0.183**          
##                               (0.080)          
##                                                
## -----------------------------------------------
## Observations                    468            
## R2                             0.403           
## Adjusted R2                    0.401           
## Residual Std. Error      1.313 (df = 466)      
## F Statistic          314.228*** (df = 1; 466)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01